package com.newhero.app.report.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;


public class StringSubStr 
{
	//截取固定长度
	public static String getSubString(String str, int length)
	{
        int count = 0;
        int offset = 0;
        char[] c = str.toCharArray();
        int size = c.length;
        if(size >= length){
            for (int i = 0; i < c.length; i++) {
                if (c[i] > 256) {
                    offset = 2;
                    count += 2;
                } else {
                    offset = 1;
                    count++;
                }
                if (count == length) {
                    return str.substring(0, i + 1);
                }
                if ((count == length + 1 && offset == 2)) {
                    return str.substring(0, i);
                }
            }
        }else{
            return str;
        }
        return "";
    }
	
	//获取截取位置
	public static int getSubPos(String str, int len)
	{
        int pos =0;
		int count = 0;
        int offset = 0; //当前字符  1非中文 2中文 汉字按一个长度，字母数字按半个长度
        char[] c = str.toCharArray();
        int size = c.length;
        if(size >= len){
            for (int i = 0; i < c.length; i++) {
                if (c[i] > 256) {  //中文 
                    offset = 2;
                    count += 2;
                } else {
                    offset = 1;
                    count++;
                }
                if (count == len)
                {
                	pos = len;
                }
                if ((count == len + 1 && offset == 2))
                {
                	pos = len-1;
                }
            }
        }
        return pos;		
	}
	
	public static String  getContent(String conStr, int sublen,int num)
	{
		String retCon = "";
		String othercon ="";
		int subpos = 0;
		if(conStr == null){
			return "";
		}
		for(int i=1;i<=num;i++)
		{
			if(conStr.length()>sublen)
			{
				subpos = getSubPos(conStr, sublen);
				retCon = conStr.substring(0, subpos);
				if(subpos==0)
				{
					othercon ="";
				}else{
					conStr = conStr.substring(subpos,conStr.length());
				}
			}else{
				if(i==num)
				{
					retCon =conStr;
				}else{
					retCon = othercon;
				}
				conStr ="";
			}
		}
		return retCon; 
	}
	//获取本地Ip
	public static String getRealIp() throws SocketException {
		String localip = null;// 本地IP，如果没有配置外网IP则返回它
		String netip = null;// 外网IP

		Enumeration<NetworkInterface> netInterfaces =
		NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		boolean finded = false;// 是否找到外网IP
		while (netInterfaces.hasMoreElements() && !finded) {
			NetworkInterface ni = netInterfaces.nextElement();
			Enumeration<InetAddress> address = ni.getInetAddresses();
		while (address.hasMoreElements()) {
			ip = address.nextElement();
			if (!ip.isSiteLocalAddress()&& !ip.isLoopbackAddress()&& ip.getHostAddress().indexOf(":") == -1) {// 外网IPnetip = ip.getHostAddress();
				finded = true;
				break;
			} else if (ip.isSiteLocalAddress()&& !ip.isLoopbackAddress()&& ip.getHostAddress().indexOf(":") == -1) {// 内网IP
				localip = ip.getHostAddress();
			}
			}
		}
		if (netip != null && !"".equals(netip)) {
			return netip;
		} else {
			return localip;
		}
	}
	public static void main(String[] args) throws SocketException 
	{
        int sublen=50;
        System.out.println(getRealIp());
		String conStr = "习近平强调，近年来，中土战略合作关系发展顺利，各领域合作取得积极进展。中方愿同土方一道努力，为中土合作带来新气象、注入新动力。双方要将我和总统先生达成的共识转化为实实在在的成果，保持各层级、各领域交往，加强政治互信，密切政策沟通，照顾彼此核心利益和重大关切，促进安全反恐合作取得突破。中土是共建“一带一路”的天然伙伴。双方要推进重要合作项目尽早落地，实现中土对接发展战略的早期收获。要深入开展人文交流。土耳其是中东地区重要新兴市场国家。中方期待同土方通过“金砖+”模式加强沟通和合作。";
        System.out.println("cont1=:"+getContent(conStr,sublen,1));
        System.out.println("cont2=:"+getContent(conStr,sublen,2));
        System.out.println("cont3=:"+getContent(conStr,sublen,3));
        System.out.println("cont4=:"+getContent(conStr,sublen,4));
	}
}
